package com.mst.service.Impl;

import com.mst.core.BusinessException;
import com.mst.domain.User;
import com.mst.mapper.UserMapper;
import com.mst.service.IUserService;
import com.mst.utils.MD5Utils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class UserServiceImpl implements IUserService {
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        User user = userMapper.getUserByUsername(username);
        if (user == null) {
            throw new BusinessException(500, "您输入的账号不存在");
        }
        if (!user.getPassword().equals(MD5Utils.md5(MD5Utils.md5(password)))) {
            throw new BusinessException(500, "密码不对");
        }
        user.setPassword(null);
        return user;
    }

    @Override
    public int register(User user) {
        User user1 = userMapper.getUserByUsername(user.getUsername());
        if (user1 != null) {
            throw new BusinessException(500, "用户已存在");
        }
        user.setPassword(MD5Utils.md5(MD5Utils.md5(user.getPassword())));
        return userMapper.insertUser(user);
    }
}
